package aishua.com.mytestapplist.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.os.Environment;
import android.util.Log;

public class LogUtils {
	private static boolean DEBUG_ON = true;
	private static boolean DEBUG_ON_TO_FILE = true;
	
	private static String TAG = "YzfApduHelper11";
	
	public static String MY_ROOT_FILE = "/"+TAG+"/Log/";
	public static String MY_LOG_FILE_NAME = "log.txt";
	public static String MY_LOG_FILE_PATH = Environment.getExternalStorageDirectory()+ MY_ROOT_FILE + MY_LOG_FILE_NAME;
//	public static String MY_LOG_FILE_PATH ="/storage/emulated/luyin.txt";


	static {
		File file = new File(Environment.getExternalStorageDirectory() + MY_ROOT_FILE);
		if (!file.exists()) {
			file.mkdirs();
		}
	}
	
	public static void showLog(boolean isLog, boolean isLog2File) {
		DEBUG_ON = isLog;
		DEBUG_ON_TO_FILE = isLog2File;
	}
	
    /**
     * Log.v
     */
	public static void v(String msg) {
		if (DEBUG_ON) {
			Log.v(TAG, msg);
			if (DEBUG_ON_TO_FILE) {
				printToFile(MY_LOG_FILE_PATH, msg);
			}
		}
	}
	
    /**
     * Log.d
     */
	public static void d(String msg) {
		if (DEBUG_ON) {
			Log.d(TAG, msg);
			if (DEBUG_ON_TO_FILE) {
				printToFile(MY_LOG_FILE_PATH, msg);
			}
		}
	}
	
    /**
     * Log.i
     */
	public static void i(String msg) {
		if (DEBUG_ON) {
			Log.i(TAG, msg);
			if (DEBUG_ON_TO_FILE) {
				printToFile(MY_LOG_FILE_PATH, msg);
			}
		}
	}
	
    /**
     * Log.w
     */
	public static void w(String msg) {
		if (DEBUG_ON) {
			Log.w(TAG, msg);
			if (DEBUG_ON_TO_FILE) {
				printToFile(MY_LOG_FILE_PATH, msg);
			}
		}
	}
	
    /**
     * 打印LOG到文件
     */
	public static void printToFile(String file, String msg) {
		if (DEBUG_ON) {
			printf(file, msg);
		}
	}
	
    /**
     * 打印异常
     */
	public static void printException (Exception e) {
		StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw, true));
        String str = sw.toString();
		w(str);
		if (DEBUG_ON_TO_FILE) {
			printExceptionToFile(e, MY_LOG_FILE_PATH);
		}
	}
	
    /**
     * 打印异常到文件
     */
	public static void printExceptionToFile (Exception e, String file) {
		StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw, true));
        String str = sw.toString();
        printToFile(file, str);
	}
	
    /**
     * 初始化 删除LOG文件
     */
	public static void initLogFile(String filePath) {
		File file = null;
		if (null != filePath) {
			file = new File(filePath);
			if (null != file && file.exists()) {
				try {
					file.delete();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
    /**
     * 打印LOG到文件
     */
	public static void printf(String filePath, String msg) {
		File file = null;
		if (null != filePath) {
			file = new File(filePath);
			if (null != file && !file.exists()) {
				try {
					file.createNewFile();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		RandomAccessFile raf = null;
		try {
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss");       
			String date = sDateFormat.format(new Date()); 
			long len = file.length();
			raf = new RandomAccessFile(file, "rwd");
			raf.seek(len);
			String logMsg = date + "=>" + msg + "\n";
			raf.write(logMsg.getBytes());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (raf != null) {
				try {
					raf.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
    static int i;
	public static int  geti(){

		return i =i+1;
	}
}
